Skip to content

Avoid unhelpful full diff for unequal range comparisons - #15154

Open
SemTiOne wants to merge 2 commits into
pytest-dev:mainfrom
SemTiOne:fix-6772-range-diff
Open

SemTiOne wants to merge 2 commits into
pytest-dev:mainfrom
SemTiOne:fix-6772-range-diff

Conversation

@SemTiOne

@SemTiOne SemTiOne commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

When comparing unequal range objects, pytest can show a full diff of their range(...) representations. This adds noise without making the mismatch any clearer.

This change skips that diff while preserving the useful sequence comparison output: the first differing index, or an extra-item summary when one range extends the other.

The shared-prefix check examines at most two items. A range's start and step determine its values, so that's enough to compare the prefix without walking through huge ranges.

Closes #6772.

  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.

Co-authored-by: OpenAI GPT-6 Luna <noreply@openai.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Oct 9, 2026
@SemTiOne
SemTiOne marked this pull request as ready for review October 9, 2026 15:56
@0xamlab

0xamlab commented Oct 11, 2026

Copy link
Copy Markdown

I checked out the branch and ran the new range tests: testing/test_assertion.py::TestAssert_reprcompare::test_range_different_lengths, test_range_different_items, and test_range_huge_comparison_is_fast all pass (3 passed in 1.67s). On main the verbose path still emits the unhelpful Full diff for range(0, 5) == range(0, 7). The early return at src/_pytest/assertion/_compare_any.py:82 cleanly skips that repr diff, and capping the prefix scan in _compare_sequence.py:56 keeps huge-range comparisons fast. Nice fix.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

assertion diffs: slightly confusing full diff for range

2 participants